﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;

using System.Text;
using Module.Common.Models;
using Module.Core.DAO;
using Module.Core.EntitiesInfo;
using Module.Core.EntitiesInfo.TablesInfo;
using System.Data;

namespace Module.Core.DAO
{
    public class NewLetterEmailsDAO
    {
        #region Fields

        protected string connectionStringName;

        #endregion

        
        #region Methods

        /// <summary>
        /// Saves a record to the NewsLetter_Emails table.
        /// </summary>
        public virtual int Insert(NewsLetterEmails newsLetterEmailsInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{

				new SqlParameter("@Date", newsLetterEmailsInfo.Date),
				new SqlParameter("@Email", newsLetterEmailsInfo.Email),
				new SqlParameter("@FullName", newsLetterEmailsInfo.FullName),
				new SqlParameter("@Address", newsLetterEmailsInfo.Address),
                new SqlParameter("@GroupId", newsLetterEmailsInfo.GroupId),

			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "NewsLetterMails_Insert", parameters);

        }

        
        #endregion

	/// <summary>
		/// Updates a record in the NewsLetter_Emails table.
		/// </summary>
		public virtual void Update(NewsLetterEmails newsLetterEmailsInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
                new SqlParameter("@EmailId",newsLetterEmailsInfo.EmailId), 
	            new SqlParameter("@Date", newsLetterEmailsInfo.Date),
				new SqlParameter("@Email", newsLetterEmailsInfo.Email),
				new SqlParameter("@FullName", newsLetterEmailsInfo.FullName),
				new SqlParameter("@Address", newsLetterEmailsInfo.Address),
                new SqlParameter("@GroupId", newsLetterEmailsInfo.GroupId)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "NewsLetterMails_Update", parameters);
		}


        /// <summary>
        /// Deletes a record from the NewsLetterMails table by its primary key.
        /// </summary>
        public virtual void Delete(int EmailId)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@EmailId", EmailId)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "NewsLetterMails_Delete", parameters);
        }


        /// <summary>
        /// Selects all records from the NewsLetterMails table.
        /// </summary>
        public CHRTList<NewsLetterEmails> SelectAll(int EmailId)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              
                                              new SqlParameter("@EmailId",EmailId)
		};
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "NewsLetterMails_Selectall", parameters))
            {
                CHRTList<NewsLetterEmails> newsLetterEmailsInfoList = new CHRTList<NewsLetterEmails>();
                while (dataReader.Read())
                {
                    NewsLetterEmails newsLetterEmailsInfo = MakenewLetterInfo(dataReader);
                    newsLetterEmailsInfoList.Add(newsLetterEmailsInfo);
                }

                return newsLetterEmailsInfoList;
            }
        }
        public CHRTList<NewsLetterEmails> SearchAll(string FullName, int from, int to, int GroupId, int EmailId)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              new SqlParameter("@FullName",FullName),
                                              new SqlParameter("@From",from),
                                              new SqlParameter("@To",to),
                                              new SqlParameter("@GroupId",GroupId),
                                              new SqlParameter("@EmailId",EmailId) 
		};
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "NewsLetterEmail_SearchAll", parameters))
            {
                CHRTList<NewsLetterEmails> newsLetterEmailsInfoList = new CHRTList<NewsLetterEmails>();
                while (dataReader.Read())
                {
                    NewsLetterEmails newsLetterEmailsInfo = MakenewLetterInfo(dataReader);
                    newsLetterEmailsInfoList.Add(newsLetterEmailsInfo);
                }

                return newsLetterEmailsInfoList;
            }
        }
        public int GetCount(string FullName, int GroupId, int EmailId)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                            {
                                                new SqlParameter("@FullName", FullName),
                                                new SqlParameter("@GroupId", GroupId),
                                                new SqlParameter("@EmailId", EmailId)
                                            };
            return
                (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "NewsLetterEmails_GetCount", parameters);

        }

        	/// <summary>
		/// Creates a new instance of the NewLetterMail class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual NewsLetterEmails MakenewLetterInfo(SqlDataReader dataReader)
		{
			NewsLetterEmails newsLetterEmailsinfo = new NewsLetterEmails();
			newsLetterEmailsinfo.EmailId = SqlClientUtility.GetInt32(dataReader,DbConstants.NewLetterEmails.EMAILID, 0);
			newsLetterEmailsinfo.GroupId = SqlClientUtility.GetInt32(dataReader,DbConstants.NewLetterEmails.GROUPID,0);
			newsLetterEmailsinfo.Date = SqlClientUtility.GetDateTime(dataReader,DbConstants.NewLetterEmails.DATE, DateTime.Now);
			newsLetterEmailsinfo.Email = SqlClientUtility.GetString(dataReader,DbConstants.NewLetterEmails.EMAIL, String.Empty);
			newsLetterEmailsinfo.FullName = SqlClientUtility.GetString(dataReader,DbConstants.NewLetterEmails.FULLNAME, String.Empty);
			newsLetterEmailsinfo.Address = SqlClientUtility.GetString(dataReader,DbConstants.NewLetterEmails.ADDRESS, string.Empty);
		    newsLetterEmailsinfo.Enable = SqlClientUtility.GetByte(dataReader,DbConstants.NewLetterEmails.ENABLE,Byte.MaxValue);
			newsLetterEmailsinfo.CreatedBy = SqlClientUtility.GetString(dataReader,DbConstants.NewLetterEmails.CREATEDBY,string.Empty);
			

			return newsLetterEmailsinfo;
		}



        /// <summary>
        /// Selects a single record from the Singer table.
        /// </summary>
        public virtual NewsLetterEmails Select(string param,int type)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Param", param),
                new SqlParameter("@Type", type)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "NewsLetterEmail_Select", parameters))
            {
                if (dataReader.Read())
                {
                    return MakenewLetterInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }

        public virtual DataTable GetGroup(int GroupId)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@GroupId", GroupId)
            };

            using (DataTable dt = SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "NewsLetterEmail_GetGroup", parameters))
            {
                return dt;
            }
        }

        public virtual string GetEmail(int GroupId)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@GroupId", GroupId)
            };

            return
                (string)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "NewsLetterEmail_GetGroup", parameters);
        }

		
    }
}